﻿using System;
using System.Windows;
using System.Windows.Data;

namespace AutoLoL.Converters
{
    public class ObjectToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // Parameter
            bool invert = false;
            if (parameter != null)
            {
                if (parameter is bool)
                {
                    invert = (bool)parameter;
                }
                else
                {
                    bool.TryParse(parameter.ToString(), out invert);
                }
            }

            Visibility result = Visibility.Collapsed;

            // Value
            if (value != null)
            {
                bool? v = true;

                if (value is bool)
                {
                    v = (bool)value;
                }
                else if (value is int)
                {
                    v = (int)value > 0;
                }
                else if (value is string)
                {
                    v = (value.ToString().Length > 0);
                }

                if (v.HasValue && v.Value)
                {
                    result = Visibility.Visible;
                }
            }

            if (!invert)
            {
                return result;
            }
            else
            {
                return (result == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
